package com.hnkywl.sts.service.cwgl.kmys;

import com.hnkywl.sts.dao.cwgl.kmys.TyfzhsmcDao;
import com.hnkywl.sts.dao.sys.JsDao;
import com.hnkywl.sts.entity.cwgl.kmys.Tyfzhsmc;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.entity.organ.Wls;
import com.hnkywl.sts.entity.sys.Js;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 说明：
 * 包:        com.hnkywl.sts.service.sys
 * 类描述:    角色服务类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class TyfzhsmcService {
    private TyfzhsmcDao tyfzhsmcDao;

    /**
     * 查询角色名称列表
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findTyfzhsmcMc(String mc, int pageNo, int pageSize) {
        Page<Tyfzhsmc> tyfzhsmcPage = this.tyfzhsmcDao.findJsList(mc, pageNo, pageSize);
        List<Tyfzhsmc> tyfzhsmcList = tyfzhsmcPage.getResult() == null ? new ArrayList<Tyfzhsmc>() : tyfzhsmcPage.getResult();
        String jsJson = BaseUtils.fetchCollectionToJson(tyfzhsmcList, "id", "mc", "code","dl");
        return "{\"total\":" + tyfzhsmcPage.getTotalCount() + ",\"rows\":[" + jsJson + "]}";

    }


    public String findAllTyfzhsmcTree(String dl) {
        return this.tyfzhsmcDao.findAllTyfzhsmcTree(dl);
    }

    public String findAllTyfzhsmcTreeJson() {
        List<Tyfzhsmc> wlsList = new ArrayList<Tyfzhsmc>();
        wlsList.addAll(tyfzhsmcDao.findAllRootMc());
        Map<String, String> treeParam = new HashMap<String, String>();
        treeParam.put("text", "mc");
        treeParam.put("id", "id");
        return BaseUtils.getTreeJson(wlsList, treeParam);
    }



    public String getTyfzhsmcdl(Long mcId) {
        List<Tyfzhsmc> list = new ArrayList<Tyfzhsmc>();
        Tyfzhsmc mc = tyfzhsmcDao.get(mcId);
        if(mc.getDl().equals("xm")){
            mc.setDl("项目");
        }
        if(mc.getDl().equals("xjllxm")){
            mc.setDl("现金流量项目");
        }
        if(mc.getDl().equals("fzhs")){
            mc.setDl("辅助核算");
        }
        list.add(mc);
        String json = BaseUtils.fetchCollectionToJson(list, "id", "dl");
        return "[" + json + "]";
    }

//    public String findAllTyfzhsmcTree() {
//        List<Tyfzhsmc> wlsList = new ArrayList<Tyfzhsmc>();
//        wlsList.addAll(tyfzhsmcDao.findAllRootMc());
//        Map<String, String> treeParam = new HashMap<String, String>();
//        treeParam.put("text", "mc");
//        treeParam.put("id", "id");
//        return BaseUtils.getTreeJson(wlsList, treeParam);
//    }

    /**
     * 查询角色明细
     *
     * @param id 角色名称
     * @return
     */
    @Transactional
    public Tyfzhsmc findTyfzhsmcDetail(Long id) {
        return this.tyfzhsmcDao.get(id);
    }

    /**
     * 保存通用辅助核算信息
     *
     * @param tyfzhsmc 通用辅助核算实体
     * @return
     */
    @Transactional
    public void saveTyfzhsmc(Tyfzhsmc tyfzhsmc) {
        tyfzhsmc.setDateCreated(new Date());
        tyfzhsmc.setLastUpdated(tyfzhsmc.getDateCreated());
        tyfzhsmc.setCreateUser(YgService.getCurrentYg());
        tyfzhsmc.setCreateOrgan(YgService.getCurrentYg().getZggs());
        tyfzhsmc.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        this.tyfzhsmcDao.save(tyfzhsmc);

    }

    /**
     * 删除角色信息
     *
     * @param id 角色ID
     * @return
     */
    @Transactional
    public void deleteTyfzhsmc(Long id) {
        Tyfzhsmc tyfzhsmc = tyfzhsmcDao.get(id);
        tyfzhsmc.setLastUpdated(new Date());
        tyfzhsmc.setDeleted(1);
        this.tyfzhsmcDao.save(tyfzhsmc);
    }

    /**
     * ajax远程验证
     *
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id, String value, String field) {
        return tyfzhsmcDao.checkUnique(id, value, field);
    }

    /**
     * 查询所有通用辅助核算名称
     *
     * @return
     */
    public List<Tyfzhsmc> findAll() {
        return tyfzhsmcDao.findAll();
    }

    @Autowired
    public void setTyfzhsmcDao(TyfzhsmcDao tyfzhsmcDao) {
        this.tyfzhsmcDao = tyfzhsmcDao;
    }
}
